1 module hip.api.data.csv;
2 
3 
4 struct HipCSVColumnRange
5 {
6     const(IHipCSV) csv;
7     private int _col = 0;
8     private int _rowIndex = 0;
9 
10     string front()
11     {
12         if(csv !is null)
13             return csv[_col, _rowIndex];
14         return "";
15     }
16     bool empty(){return _col >= csv.columns || _rowIndex >= csv.rows;}
17     void popFront()
18     {
19         _rowIndex++;
20     }
21 }
22 
23 /**
24 *   Usage:
25 ```d
26 //Iterate every value
27 foreach(v; csv) //or
28 //Iterate columns
29 foreach(v; csv.getColumnRange(0)) //or
30 //Iterate rows
31 foreach(v; csv.getRow(0))
32 //Get the csv cell
33 csv[x, y]
34 ```
35 */
36 interface IHipCSV
37 {
38     size_t rows() const;
39     size_t columns() const;
40     string opIndex(size_t x, size_t y) const;
41     const(string[]) getRow(size_t row) const @nogc;
42     final HipCSVColumnRange getColumnRange(size_t column, size_t startRow = 0) const @nogc
43     {
44         return HipCSVColumnRange(this, cast(int)column, cast(int)startRow);
45     }
46     string path() const;
47 
48     final int opApply(scope int delegate(string) dg) const
49     {
50         size_t r = rows();
51         size_t c = columns();
52         int result;
53 
54         outer: for(int i = 0; i < r; i++)
55             for(int j = 0; j < c; j++)
56             {
57                 result = dg(this[j, i]);
58                 if(result)
59                     break outer;
60             }
61         return result;
62     }
63 }